#include "ace.h"

byte buffer[512];

void setup(void)
{
  Serial.begin(19200);
  
  // AKA PORTB-0
  //
  pinMode(8,INPUT);
  pinMode(9,OUTPUT);
  pinMode(10,OUTPUT);
}

void loop(void)
{
/*
  pinMode(3,OUTPUT);
  digitalWrite(3,LOW);
  pinMode(4,OUTPUT);
  digitalWrite(4,LOW);
  pinMode(5,OUTPUT);
  digitalWrite(5,LOW);
  pinMode(6,OUTPUT);
  digitalWrite(6,LOW);
  if (detectLeadIn(500))
  {
    digitalWrite(6,HIGH);
    digitalWrite(4,LOW);
  }
  else
  {
    digitalWrite(4,HIGH);
    digitalWrite(6,LOW);
  }
*/  

  Serial.println("T(est), L(oad)");
  Serial.print("Choice?");

  for (;;)
  {
    if (Serial.available())
    {
      int c = Serial.read();
      if (c == 't' || c == 'T')
      {
        Serial.println("");
        Serial.println("");
        TestMode();
        break;
      }
      else if (c == 'l' || c == 'L')
      {
        Serial.println("");
        Serial.println("");
        LoadMode();
        break;
      }
    }
  }
}


void TestMode(void)
{
  Serial.println("Start a lead-in tone, send 'Q' to quit.");

  for (;;)
  {
    if (Serial.available() && (Serial.read()|32) == 'q')
    {
      Serial.println("OK.");
      return;
    }

    if (detectLeadIn(500))
    {
      Serial.println("Lead-in detected.");
    }
    else
    {
      Serial.println("Waiting for lead-in.");
    }
  }
}

void LoadMode(void)
{
  Serial.println("Press play on tape..");

  int returnCode;
  if (!loadFile(12000, buffer, NULL, returnCode))
  {
    Serial.print("Error ");
    return Serial.println(returnCode, DEC);
  }

  Serial.println("File loaded OK");
}
